//
//  singerTableViewController.m
//  B项目
//
//  Created by lanou on 16/8/9.
//  Copyright © 2016年 Work. All rights reserved.
//

#import "singerTableViewController.h"
#import "PrefixHeader.pch"
#import "musicModel.h"
#import "PlayMusicViewController.h"
#import "musicManager.h"
@interface singerTableViewController ()

@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation singerTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"歌曲列表";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //模糊视图
    UIImage *backImg = [UIImage imageNamed:@"11471923,2560,1600.jpg"];
    UIImageView *imgV = [[UIImageView alloc]initWithImage:backImg];
    self.tableView.backgroundView = imgV;

    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleLight)];
    UIVisualEffectView *effectivew = [[UIVisualEffectView alloc]initWithEffect:blur];
    effectivew.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [imgV addSubview:effectivew];
    //数据请求
    [self dataRequest];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   }

//懒加载
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];

    }

    return _dataArray;
}


//数据解析
- (void)dataRequest
{
    NSString *urlStringOne = @"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.artist.getSongList&format=json&tinguid=";
    NSString *urlStringTwo = @"&artistid(null)&limits=30&order=2&offset=0&version=5.2.5&from=ios&channel=appstore";
    NSString *urlString = [NSString stringWithFormat:@"%@%@%@",urlStringOne,self.ting_uID,urlStringTwo];

    
    [NetWorkRequestManager requestWithType:GET urlString:urlString parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dic in dict[@"songlist"]) {
            musicModel *model = [[musicModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });

    } finishError:nil];
    
    
}






//头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    [imgView sd_setImageWithURL:[NSURL URLWithString:self.headImg] placeholderImage:[UIImage imageNamed:@"ic_blank_collect@3x.png"]];
    [view1 addSubview:imgView];
    return view1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 200;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    musicModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@----%@",model.title,model.author];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    return cell;
}



//点击进入播放页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    PlayMusicViewController *playMusicVC = [[PlayMusicViewController alloc]init];
    [musicManager shareInstance].musicArray = self.dataArray;
    [musicManager shareInstance].index = indexPath.row;
    [self.navigationController presentViewController:playMusicVC animated:YES completion:nil];
        [[musicManager shareInstance]loadSongInfo];



}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
